import moment from 'moment-timezone';
import { 
  biScreenColors, 
  chartColors, 
  getGradientColor, 
  getChartColor, 
  getStatusColor as getThemeStatusColor 
} from '../components/ThemeToggle/theme';

class Utils {
  static isDev = process.env.NODE_ENV === 'development';

  // 更新主题背景获取 - 使用新的BI配色方案
  static getThemeBg(theme = true) {
    return theme ? {
      backgroundColor: biScreenColors.cardBg,
      color: biScreenColors.text.primary 
    } : {
      backgroundColor: '#ffffff',
      color: '#000000' 
    }
  }

  // 使用新的图表颜色系统
  static getRandomColor(index: string) {
    return getChartColor(parseInt(index), 'blues');
  }

  // 获取图表颜色 - 使用新的配色方案
  static getColors() {
    return chartColors.primary;
  }

  // 新增：获取BI大屏渐变色
  static getBIGradientColor(index: number) {
    return getGradientColor(index);
  }

  // 新增：获取状态颜色
  static getStatusColor(status: 'online' | 'offline' | 'warning' | 'normal') {
    return getThemeStatusColor(status);
  }

  // 新增：获取排行榜颜色
  static getRankingColor(rank: number) {
    if (rank === 1) return biScreenColors.ranking.gold;
    if (rank === 2) return biScreenColors.ranking.silver;
    if (rank === 3) return biScreenColors.ranking.bronze;
    return 'transparent';
  }

  // 新增：生成卡片样式
  static getCardStyle(hover = true) {
    return {
      backgroundColor: biScreenColors.cardBg,
      border: `1px solid ${biScreenColors.border.muted}`,
      borderRadius: '8px',
      boxShadow: '0 2px 8px rgba(3, 210, 253, 0.1)',
      ...(hover && {
        transition: 'all 0.3s ease',
        cursor: 'pointer'
      })
    };
  }

  // 新增：生成按钮样式
  static getButtonStyle(type: 'primary' | 'secondary' = 'primary') {
    return {
      backgroundColor: type === 'primary' 
        ? biScreenColors.button.primary 
        : biScreenColors.button.secondary,
      border: `1px solid ${biScreenColors.button.border}`,
      color: type === 'primary' ? '#ffffff' : biScreenColors.primary,
      borderRadius: '6px',
      padding: '8px 16px',
      cursor: 'pointer',
      transition: 'all 0.3s ease'
    };
  }

  static *bubbleSort(arr: number[]) {
    const n = arr.length;
    for (let i = 0; i < n - 1; i++) {
      for (let j = 0; j <= n - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
          [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
        }
        yield arr.map((a, i) => ({
          value: a,
          swap: i === j || i === j + 1,
        }));
      }
    }
    return arr;
  }

  static *selectionSort(arr: number[]) {
    for (let i = 0; i < arr.length; i++) {
      let lowest = i;
      for (let j = i + 1; j < arr.length; j++) {
        if (arr[j] < arr[lowest]) lowest = j;
      }
      if (lowest !== i) {
        [arr[i], arr[lowest]] = [arr[lowest], arr[i]];
      }
      yield arr.map((a, index) => ({
        value: a,
        swap: index === lowest || index === i,
      }));
    }
    return arr;
  }

  static *insertionSort(arr: number[]) {
    const len = arr.length;
    let preIndex:number, current;
    for (let i = 1; i < len; i++) {
      preIndex = i - 1;
      current = arr[i];
      while (preIndex >= 0 && arr[preIndex] > current) {
        arr[preIndex + 1] = arr[preIndex];
        preIndex--;
      }
      arr[preIndex + 1] = current;
      yield arr.map((a, index) => ({
        value: a,
        swap: index === preIndex + 1 || index === i,
      }));
    }
    return arr;
  }

  static sleep(time = 4000) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(1);
      }, time)
    })
  }

  static memoryManage: IMemory = {
    data: null,
    get(key: string) {
      return this.data ? this.data[key] : null
    },
    set(key: string, value) {
      if (!this.data) {
        this.data = {};
      }
      this.data[key] = value;
    },
    clear() {
      this.data = null;
    },
    remove(key: string) {
      this.data[key] = null;
      delete this.data[key];
    }
  }

  static getCurrentTimeInShanghai(): string {
    return moment().tz('Asia/Shanghai').format("YYYY-MM-DD HH:mm:ss");
  }

  static getCurrentDayInShanghai() {
    return moment().tz('Asia/Shanghai').format("YYYY-MM-DD");
  }
}

interface IMemory {
  data: any,
  get: (key: string) => any,
  set: (key: string, value: any) => void,
  clear: () => void,
  remove: (key: string) => void
}


export const {
  getThemeBg,
  bubbleSort,
  selectionSort,
  insertionSort,
  sleep,
  isDev,
  memoryManage,
  getCurrentTimeInShanghai,
  getRandomColor,
  getColors,
  getCurrentDayInShanghai,
  // 新增的颜色和样式工具
  getBIGradientColor,
  getStatusColor,
  getRankingColor,
  getCardStyle,
  getButtonStyle
} = Utils;
